
import { http } from './index'
import { ElMessage } from 'element-plus'

// 登陆方法
export function login(data) {
    return http('login', 'POST', data).then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg + res.data.username,
            type: 'success'
        })
        return res.data
    })
}

// 获取用户列表
export function getUsers(params) {
    return http('users', 'GET', {}, params).then(res => {
        return res.data
    })
}

// 获取左侧导航条
export function getNavAside() {
    return http('menus', 'GET').then(res => {
        return res.data
    })
}

// 获取角色列表
export function getRoles() {
    return http('roles', 'GET').then(res => {
        return res.data
    })
}

// 添加用户
export function addUsers(data) {
    return http('users', 'POST', data).then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data
    })
}
// 修改用户状态
export function changeUserStatus(uid, type) {
    return http(`users/${uid}/state/${type}`, 'PUT').then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data
    })
}
// 编辑用户信息
export function edidUserInfo(id) {
    return http(`users/${id}`, 'GET').then(res => {
        return res.data
    })
}
// 编辑用户提交
export function submitEdidUserInfo(id, { email, mobile }) {
    return http(`users/${id}`, 'PUT', { email, mobile }).then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data
    })
}
// 删除单个用户
export function deleteUser(id) {
    return http(`users/${id}`, 'DELETE').then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data
    })
}
// 分配角色
export function assignRoles(id, rid) {
    return http(`users/${id}/role`, 'PUT', { id, rid }).then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data;
    })
}
// 根据用户id查询用户信息
export function getUsersByID(id) {
    return http(`users/${id}`, 'GET').then(res => {
        return res.data;
    })
}

// 添加角色
export function addRoles(data) {
    return http(`roles`, 'POST', data).then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data;
    })
}

// 根据 ID 查询角色
export function getRolesById(id) {
    return http(`roles/${id}`, 'GET').then(res => {
        return res.data;
    })
}
// 编辑提交角色
export function editRolesById(id, data) {
    return http(`roles/${id}`, 'PUT', data).then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data;
    })
}
// 删除角色
export function deleteRoles(id) {
    return http(`roles/${id}`, 'DELETE').then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data;
    })
}
// 角色授权
export function rolesAuthorize(id, rids) {
    return http(`roles/${id}/rights`, 'POST', rids).then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data;
    })
}

// 删除角色权限
export function deleteRolesRight(roleId, rightId) {
    return http(`roles/${roleId}/rights/${rightId}`, 'DELETE').then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data;
    })
}

// 获取权限列表

export function getRights(){
    return http(`rights/list`,'GET').then(res=>{
       return res.data 
    })
}
//权限列表 
export function getTree(){
    return http('rights/tree','GET').then(res=>{
        return res.data
    })
}

// 获取商品列表
export function getGoods(params) {
    return http('goods', 'GET', {}, params).then(res => {
        return res.data
    })
}

//  根据 ID 查询商品
export function getGoodsById(id) {
    return http(`goods/${id}`, 'GET').then(res => {

        return res.data
    })
}
// 编辑提交商品
export function upEditGoods(data) {
    return http(`goods/${data.id}`, 'PUT', data).then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data
    })
}
// 删除商品
export function deleteGoods(id) {
    return http(`goods/${id}`, 'DELETE').then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data
    })
}

// 获取商品分类
export function getCategories(params){
    return http('categories','GET',{},params).then(res=>{
       return res.data 
    })
}
// 获取订单列表
export function getOrders(params) {
    return http('orders', 'GET', {}, params).then(res => {
        return res.data
    })
}

// 获取数据报表数据
export function getReports() {
    return http('reports/type/1', 'GET').then(res => {
        return res.data
    })
}

// 获取参数列表
export function getActiveList(id, type) {
    return http(`categories/${id}/attributes`, 'GET', {}, {
        sel: type
    }).then(res => {
        return res.data
    })
}
// 添加商品
export function addGoods(data) {
    return http(`goods`, 'POST', data).then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data
    })
}

// 添加动态参数或者静态属性
export function addAttributes(id, data) {
    return http(`categories/${id}/attributes`, 'POST', data).then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data
    })
}
// 编辑提交参数
export function editSubmitAttrs(id, data) {
    console.log(data);
    return http(`categories/${id}/attributes/${data.attr_id}`, 'PUT', data).then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data
    })
}

// 删除参数
export function deleteCategories(id, attrId) {
    return http(`categories/${id}/attributes/${attrId}`, 'DELETE').then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data
    })
}
// 添加分类

export function addCategory(data) {
    return http(`categories`, 'POST', data).then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data
    })
}
// 编辑提交参数

export function updataCate(id,data){
    return http(`categories/${id}`,'PUT',data).then(res=>{
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data
    })
}
// 删除分类
export function deleteCate(id) {
    return http(`categories/${id}`, 'DELETE').then(res => {
        ElMessage({
            showClose: true,
            message: res.meta.msg,
            type: 'success'
        })
        return res.data
    })
}
// 提交修改订单

export function updataOrder(data){
    return http(`orders/${data.id}`,'PUT',data).then(res=>{
        ElMessage({
            showClose:true,
            message:res.meta.msg,
            type:'success'
        })
        return res.data 
    })
}
